import { trigger, track } from "./effect.js"

const isObject = (target: any) => target != null && typeof target == "object"

export const reactive = <T extends object>(target: T) => {
  return new Proxy(target, {
    get (target, key, receiver) {
      let res = Reflect.get(target, key, receiver)  // receiver保证上下文this引用一致
      
      track(target, key)

      if(isObject(res))  {
        return reactive(res)
      }

      return res
    },
    set (target, key, value, receiver) {
      let res = Reflect.set(target, key, value, receiver)

      trigger(target, key)

      return res
    }
  })
}